package com.nbot.common;

import com.nbot.common.command.BaseCommand;
import com.nbot.common.command.BotCommand;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * 指令解释器工厂
 */
public class InterpreterFactory {
    /**
     * 根据参数command，去生成一个该命令的解释器
     *
     * @param command
     * @return
     * @throws ClassNotFoundException
     * @throws NoSuchMethodException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     * @throws InstantiationException
     */
    public static Interpreter getInterpreter(BaseCommand command) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        String cName = command.getClass().getSimpleName();
//        System.err.println(cName);
//        System.err.println(command.getClass().getSimpleName());
//        System.err.println(cName + "Interpreter");
        String tmpStr = null;
        if (command.getType() / BotCommand.BOT_TO_SERVER_TYPE == 1) {
            tmpStr = Interpreter.S_INTERPRETER_CLASS_PREX;
        } else if (command.getType() / BotCommand.SERVER_TO_BOT_TYPE == 1) {
            tmpStr = Interpreter.C_INTERPRETER_CLASS_PREX;

        }
        Class clazz = Class.forName(tmpStr + cName + "Interpreter");
//        Constructor constructor = clazz.getConstructor(null);
        Constructor constructor = clazz.getConstructor(BaseCommand.class);
        Interpreter result = (Interpreter) constructor.newInstance(command);

        return result;

    }

}
